﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using System.Threading;
using WFNetLib.StringFunc;

namespace 生命体征测试仪
{
    public partial class formSetUseCount : formBase
    {
        public formSetUseCount()
        {
            InitializeComponent();
        }
        byte[] Encode;
        int x;
        private void formSetUseCount_Load(object sender, EventArgs e)
        {
            Random r = new Random();
            x=r.Next(1, 100);
            string str1 = ReadSystemHardDeviceParam.GetHardDisk_SerialNumber().Trim();
            string str2 = ReadSystemHardDeviceParam.GetCPU_SerialNumber().Trim();
            string str3 = ReadSystemHardDeviceParam.GetMacAddrs().Trim();
            string str4 = ReadSystemHardDeviceParam.GetMasterBoard_SerialNumber().Trim();
            byte[] b1 = Encoding.ASCII.GetBytes(str1);
            byte[] b2 = Encoding.ASCII.GetBytes(str2);
            byte[] b3 = Encoding.ASCII.GetBytes(str3);
            byte[] b4 = Encoding.ASCII.GetBytes(str4);
            byte[] ID = new byte[8];
            for (int i = 0; i < 8; i++)
            {
                ID[i] = (byte)(b1[i] ^ b2[i] ^ b3[i] ^ b4[i]);
            }
            lcyHashCal lcy = new lcyHashCal();
            Encode = lcy.HashCal(ID, x);
            tbKey.Text = StringsFunction.byteToHexStr(Encode, " ");
            tbCount.Text="";
            ProcessHelper.StartProcess("osk.exe");
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void formSetUseCount_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ProcessHelper.IsRunningProcess("osk"))
            {
                ProcessHelper.CloseProcess("osk");
            }
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            byte[] getstr;
            try
            {
                getstr=StringsFunction.strToHexByte(tbCount.Text, " ");
            }
            catch { MessageBox.Show("输入的注册码有问题，请检查!!"); return; }
            if (getstr.Length != 9)
            {
                MessageBox.Show("输入的注册码有问题，请检查!!"); 
                return;
            }
            byte[] ret = new byte[8];
            for (int i = 0; i < 4; i++)
                ret[i] = getstr[i];
            for (int i = 0; i < 4; i++)
                ret[i+4] = getstr[i+5];
            byte n = getstr[4];
            n = (byte)(n ^ 'f');
            n = (byte)(n ^ 'w');
            lcyHashCal lcy = new lcyHashCal();
            byte[] check = lcy.HashCal(Encode, n);
            for (int i = 0; i < 8; i++)
            {
                if (ret[i] != check[i])
                {
                    MessageBox.Show("输入的注册码有问题，请检查!!");
                    return;
                }
            }
            SoftDataProtect.SetSoftData("0", 0);
            SoftDataProtect.SetSoftData((n-1).ToString(), 1);
            this.Close();
        }
    }
}
